package com.gdth.bank.model;

import java.util.List;

import com.gdth.bank.service.TAppMediaService;
import com.gdth.database.bean.TAppMedia;

import rx.Observable;
import rx.Subscriber;

/** 
 * @author  Arvin: 
 * @version 2016年5月27日 上午10:30:23 
 * 
 */
public class MediaModel {
	/**  **/
	TAppMediaService mMediaService;
	
	public Observable<TAppMedia> save(final TAppMedia mMedia){
		return Observable.create(new Observable.OnSubscribe<TAppMedia>() {

			@Override
			public void call(Subscriber<? super TAppMedia> arg0) {
				// TODO Auto-generated method stub
				
				mMediaService = new TAppMediaService();
				Long id = mMediaService.save(mMedia);
				
				if (id < 1){
					mMedia.setId(id);
					
					arg0.onNext(mMedia);
					arg0.onCompleted();
				} else{
					arg0.onError(null);
				}
			}
		});
	}
	
	public Observable<List<TAppMedia>> getAllMedia(){
		return Observable.create(new Observable.OnSubscribe<List<TAppMedia>>() {

			@Override
			public void call(Subscriber<? super List<TAppMedia>> arg0) {
				// TODO Auto-generated method stub
				mMediaService = new TAppMediaService();
				
				List<TAppMedia> list = mMediaService.loadAll();
				
				if (list != null){
					arg0.onNext(list);
					arg0.onCompleted();
				}else{
					arg0.onError(null);
				}
			}
		});
	}
}
